<<template>
  <div class="login">
    <van-nav-bar title="登录" />
    <van-form @submit="onSubmit">
      <van-field
        v-model="mobile"
        name="mobile"
        label="手机号"
        placeholder="请输入手机号"
        :rules="rules.mobile"
      />
      <van-field
        v-model="code"
        name="code"
        label="验证码"
        placeholder="请输入验证码"
        :rules="rules.code"
      />
      <div style="margin: 16px;">
        <van-button
          round
          block
          type="info"
          native-type="submit"
          :disable="isLoading"
          :loading="isLoading"
          loading-type="spinner"
          loading-text="正在登陆..."
          >登录</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import axios from 'axios'

import Vue from 'vue'
import { Toast } from 'vant'
Vue.use(Toast)
export default {
  data () {
    return {
      isLoading: false,
      mobile: '',
      code: '',
      rules: {
        mobile: [
          { required: true, message: '手机号不能为空' },
          {
            pattern: /^((13[\d])|(14[57])|15[356789])|(17[067])|(18[012356789])\d{8}$/,
            message: '请输入正确的手机号',
            trigger: 'onBlur'
          }
        ],
        code: [
          { required: true, massage: '验证码不能为空' },
          { pattern: /^\d{6}$/, message: '请输入六位数字验证码' }
        ]
      }
    }
  },
  methods: {
    onSubmit (val) {
      this.isLoading = true
      axios({
        url: 'http://toutiao-app.itheima.net/v1_0/authorizations',
        method: 'post',
        data: {
          mobile: val.mobile,
          code: val.code
        }
      })
        .then(res => {
          //   this.isLoading = false
          this.$toast({
            type: 'success',
            message: '登陆成功'
          })
        })
        .catch(error => {
          //   this.isLoading = false
          this.$toast({
            type: 'fail',
            message: '登陆失败'
          })
        })
        .finally(() => {
          this.isLoading = false
        })
    }
  }
}
</script>

<style scoped lang="less"></style>
